package com.sunseagear.wind.modules.sys.json;

import com.sunseagear.common.utils.ObjectUtils;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.common.utils.UserUtils;
import com.sunseagear.wind.modules.sys.entity.User;
import com.sunseagear.wind.modules.sys.entity.query.SysUserQuery;
import com.sunseagear.wind.modules.sys.service.IUserService;
import com.sunseagear.wind.modules.sys.service.impl.OrganizationServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 *
 * @version V1.0
 * @package com.sunseagear.wind.modules.sys.controller
 * @title: 用户信息控制器
 * @description: 用户信息控制器 * @date: 2018-09-03 15:10:10
 * @copyright: 2018 www.sunseagear.com Inc. All rights reserved.
 */

@RestController
@RequestMapping("json/user")
@Log(title = "用户管理")
@RequiredArgsConstructor
public class UserJsonController extends BaseBeanController {
    private final IUserService userService;
    private final OrganizationServiceImpl organizationService;

    @PostMapping(value = "list")
    @Log(logType = LogType.SELECT)
    public RPage<User> list(SysUserQuery query) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t.tenant_id", UserUtils.getTenantId());
        queryWrapper.like(ObjectUtils.isNotEmpty(query.getRealname()), "t.realname", query.getRealname());
        queryWrapper.like(ObjectUtils.isNotEmpty(query.getUsername()), "t.username", query.getUsername());
        queryWrapper.like(ObjectUtils.isNotEmpty(query.getPhone()), "t.phone", query.getPhone());
        queryWrapper.in(ObjectUtil.isNotEmpty(query.getOrganizationId()), "t.organization_id", organizationService.getChildrenIdsAndSelf(query.getOrganizationId()));
        // 预处理
        Page<User> pageBean = userService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    @PostMapping("login")
    public R<User> login(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
        User user = userService.findByUsername(username);
        if (user == null) {
            return R.fail("请联系后台管理员");
        }
        Boolean result = userService.checkPassword(user.getId(), password);

        if (result) {
            return R.success("登陆成功!", user);
        }
        return R.fail("账户名或密码不对");
    }

    @PostMapping(value = "changePassword")
    @Log(logType = LogType.OTHER, title = "修改成功")
    public R<Void> changePassword(Long id, String password, String oldPassword) {
        if (userService.checkPassword(id, oldPassword)) {
            userService.changePassword(id, password);
            return R.success("密码修改成功");
        } else {
            return R.success("旧密码错误");
        }
    }

    @GetMapping(value = "/detail/{id}")
    @Log(logType = LogType.SELECT)
    public R<User> selectUserById(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        return R.success(user);
    }

}
